package com.paireach.szwolaiyun.sz_util;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

/**
 * 用于本地缓存的帮助类
 * Created by Administrator on 2016/6/20.
 */
public class SharedPreferencesUtil {
    public static final String APP_SP_NAME = "Sz_haolaiyun";
    //用户手机号
    public static final String USER_PHONE = "user_phone";
    //用户密码
    public static final String USER_PASSWORD = "user_password";
    //用户名
    public static final String USER_NAME = "name";

    public static final String REAL_NAME = "real_name";

    //异常电话
    public static final String HOTLINE = "hotline";

    public static final String ICON = "icon";

    public static final String ID_CARD = "id_card";

    public static final String ID = "id";

    public static final String ALIAS = "alias";


    //返点时间间隔
    public static final String POINT_BCAK_TIME = "pointBack";
    //装运工
    public static final String STEVEDORES_NAME = "stevedoresName";


    private static SharedPreferences sharedPreferences;

    private static SharedPreferences getInstance(Context context) {
        if (null == sharedPreferences) {
            sharedPreferences = context.getSharedPreferences(APP_SP_NAME, Context.MODE_PRIVATE);
        }
        return sharedPreferences;
    }

    /**
     * 是否登录，如果登录，返回true
     *
     * @return
     */
    public static boolean isLogin(Context context) {
        if (TextUtils.isEmpty(getValue(context, USER_PHONE)) ) {
            return false;
        }
        return true;
    }

    ;

    /**
     * 取出本地储存的value
     *
     * @param context 上下文
     * @param key     键
     * @return 值
     */
    public static String getValue(Context context, String key) {
        return getInstance(context).getString(key, "");
    }

    /**
     * 记录String字符串到本地
     *
     * @param context 上下文
     * @param key     键
     * @param value   值
     */
    public static void setValue(Context context, String key, String value) {
        getInstance(context).edit().putString(key, value).commit();
    }

    /**
     * 用户注销
     *
     * @param context
     */
    public static void logout(Context context) {
        setValue(context, USER_PHONE, "");
        setValue(context, USER_PASSWORD, "");
    }

    public static void login(Context context, String name, String psd) {
        setValue(context, USER_PHONE, name);
        setValue(context, USER_PASSWORD, psd);
    }
}
